package scriptrunner.execution;

import java.io.Serializable;

public class ValueStringObject extends ValueObject implements Serializable,
		Cloneable, Addable, ComparableValue {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2417973034633848002L;

	public ValueStringObject() {
		this.value = null;
	}

	public ValueStringObject(String s) {
		this.value = s.substring(0);
	}

	@Override
	public ValueStringObject clone() throws CloneNotSupportedException {
		ValueStringObject o = (ValueStringObject) super.clone();
		o.value = value.substring(0);
		return o;
	}

	public String GetValue() {
		return value;
	}

	private String value;

	@Override
	public ValueStringObject Add(Addable o) throws ExecuteErrorException {
		if (o.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueStringObject result = new ValueStringObject();
		result.value = value + ((ValueStringObject) o).value;
		return result;
	}

	@Override
	public int AddTypePerference() {
		return 2;
	}

	@Override
	public void ParseFromString(String s) throws ExecuteErrorException {
		value = s.substring(0);
	}

	@Override
	public String SaveValueToString() {
		return value.substring(0);
	}

	@Override
	public boolean LessThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueStringObject rhsValue = (ValueStringObject) rhs;

		return value.compareTo(rhsValue.value) < 0;
	}

	@Override
	public boolean GreaterThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueStringObject rhsValue = (ValueStringObject) rhs;

		return value.compareTo(rhsValue.value) > 0;
	}

	@Override
	public boolean LessEqThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueStringObject rhsValue = (ValueStringObject) rhs;

		return value.compareTo(rhsValue.value) <= 0;
	}

	@Override
	public boolean GreaterEqThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueStringObject rhsValue = (ValueStringObject) rhs;

		return value.compareTo(rhsValue.value) >= 0;
	}

	@Override
	public boolean EqualTo(EqualAndNotEqual rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueStringObject rhsValue = (ValueStringObject) rhs;

		return value.equals(rhsValue.value);
	}

	@Override
	public boolean NotEqualTo(EqualAndNotEqual rhs)
			throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueStringObject rhsValue = (ValueStringObject) rhs;

		return !value.equals(rhsValue.value);
	}
	
	@Override
	public boolean equals(Object o) {
		if ( o == null || o.getClass() != getClass())
			return false;
		ValueStringObject rhs = (ValueStringObject)o;
		return value.equals(rhs.value) ;
	}
	
	@Override
	public String toString() {
		return value.substring(0);
	}
}
